package com.tank.net.msg;

import java.util.Objects;

/**
 * 消息类型
 * 消息分类较细，发的无用消息越少，服务器压力越小。消息分类很有必要
 * @author CaoBaoYin
 * @since 21/11/3 16:50
 */
public enum MsgTypeEnum {

    TANK_JOIN, TANK_START_MOVING, TANK_DIRECTION_CHANGE, TANK_STOPPED, BULLET_NEW, TANK_DIE;

    // 首字母没有大写
    /*private static Pattern linePattern = Pattern.compile("_(\\w)");
    *//** 下划线转驼峰 *//*
    public String hump () {
        String str = this.toString().toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }*/

    public String hump () {
        String lowerCase = this.toString().toLowerCase();
        String[] split = lowerCase.split("_");
        StringBuffer sb = new StringBuffer();
        for (String s : split) {
            sb.append(toFirstUpperCase(s));
        }
        return sb.toString();
    }

    private String toFirstUpperCase(String s) {
        Objects.requireNonNull(s, "字符串为null");
        if (s.trim().length() < 1) {
            throw new IllegalArgumentException("字符串为空");
        }

        char[] chars = s.toCharArray();
        chars[0] -= 32;
        return String.valueOf(chars);
    }
}
